package com.wl.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.util.MD5Util;
import com.wl.entity.Dept;
import com.wl.entity.Emp;
import com.wl.service.AdminService;
import com.wl.service.EmpService;
import com.wl.util.CommonMessage;
import com.wl.util.CpachaUtil;
import com.wl.util.PageData;

@Controller
public class EmpController {
	//注入service
	@Autowired
	private EmpService empService;
	@Autowired
	private AdminService adminService;
	
	/**
	 * 根据empno查询信息
	 * @param empno
	 * @return
	 */
	@GetMapping("selemp/{empno}")
	@ResponseBody
	public Emp selEmp(@PathVariable("empno") String empno) {
		Emp emp = empService.selectEmpById(empno);
		System.out.println("emp:"+emp.getEmpname());
		return emp;
		
	}

	
	/**
	 * 修改
	 * @return
	 */
	@PutMapping(value = "updateemp",produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateDept(@RequestBody Emp emp) {
		System.out.println(emp);
		String psw = "";
		if (emp.getEmppwd() != null) {
			// 使用MD5算法将密码转换成密文
			psw = MD5Util.getEncodeByMd5(emp.getEmppwd());
		}
		emp.setEmppwd(psw);
		System.out.println("***********************************");
		System.out.println(emp);
		boolean flag = empService.updateByEmpno(emp);
		return flag;
	}
	
	
	
	
	/**
	 * 根据empno删除信息
	 * @param empno
	 * @return true/false
	 */
	@GetMapping(value ="delemp/{empno}" ,produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean delEmp(@PathVariable("empno") String empno) {
		boolean flag = empService.deleteByEmpno(empno);
		System.out.println("flag:"+flag);
		return flag;
		
	}
	
	/**
	 * 此时返回的数据格式，是我们自己定义PageData; 和前端要有约定，双发要之前就要达成协议;json格式一致
	 * 
	 * @param page
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@GetMapping(value = "emppage111", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Emp> listEmpByPage(@RequestParam(value="page",defaultValue="1") int page, @RequestParam(value="limit",defaultValue="10") int limit, @RequestParam(value="keywords",defaultValue="") String keywords) {
		return empService.selectEmpByLike(page, limit, keywords);
	}
	
	
	/**
	 * 多功能动态查询 --------测试
	 * @param page
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@GetMapping(value = "emppage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Emp> selectByPageS(@RequestParam(value="page",defaultValue="1") int page, @RequestParam(value="limit",defaultValue="10") int limit, @RequestParam(value="keywords",defaultValue="") String keywords, @RequestParam(value="keywords2",defaultValue="") String keywords2, @RequestParam(value="keywords3",defaultValue="") String keywords3, @RequestParam(value="keywords4",defaultValue="") String keywords4, @RequestParam(value="keywords5",defaultValue="") String keywords5) {
		return empService.selectEmpByLikePageS(page, limit, keywords, keywords2, keywords3, keywords4, keywords5);
	}
	
	
	 
	/**
	 * 添加信息
	 * @return
	 */
	@PostMapping(value = "addemp",produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean addemp(@RequestBody Emp emp) { // 创建对象
		System.out.println(emp);
		String psw = "";
		if (emp.getEmppwd() != null) {
			// 使用MD5算法将密码转换成密文
			psw = MD5Util.getEncodeByMd5(emp.getEmppwd());
		}
		emp.setEmppwd(psw);
		System.out.println("***********************************");
		System.out.println(emp);
		boolean flag = empService.addEmp(emp);
		return flag;
	}
	
	/**
	 * 登录验证
	 * @param emp
	 * @param status
	 * @param request
	 * @return
	 */
	@PostMapping(value="loginemp")
	public String checkLogin(Emp emp,Integer status, HttpServletRequest request){
		System.out.println(emp);
		boolean flag=false;
		Emp emp2 = null;
		if (status == 0) {
			flag = empService.checkLogin(emp.getEmpno(),emp.getEmppwd());
			emp2 = empService.getEmpByEmp(emp);
			System.out.println(emp2);
		}else {
			flag = adminService.checkLoginByAdmin(emp.getEmpno(), emp.getEmppwd());
		}
		
		 if(flag==true) {
			 if (status ==0) {
				 request.getSession().setAttribute("power", emp2.getEmppos());
				 request.getSession().setAttribute("empno", emp2.getEmpno());
				 request.getSession().setAttribute("emp2", emp2);
			}else {
				 request.getSession().setAttribute("power", 100);	
				 request.getSession().setAttribute("empno","admin");
			}
			
			return "forward:index.jsp";
		 }else {
			request.setAttribute("errMsg", "您输入的账号或密码错误！！");
			return "forward:login.jsp";
		 }
	}
	
	/**
	 * 登录跳转
	 * @param request
	 * @return
	 */
	@GetMapping(value="login")
	public String checkLogin(HttpServletRequest request){
		
			return "login";
	}
	
	@GetMapping(value = "index")
	public String toLogin() {
		
		return "index";
	}
	
	/**
	 * 动态修改
	 * @return
	 */
	@PutMapping(value = "updateSQL",produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updatEmpSQL(@RequestBody Emp emp) {
		System.out.println(emp);
		boolean flag = empService.updateByKey(emp);
		return flag;
	}
	
	
	
	/**
	 * 显示 验证码
	 * @param request
	 * @param vl
	 * @param w
	 * @param h
	 * @param response
	 */
	@RequestMapping(value="/get_cpacha",method=RequestMethod.GET)
	public void getCpacha(HttpServletRequest request,
			@RequestParam(value="vl",defaultValue="4",required=false) Integer vl,
			@RequestParam(value="w",defaultValue="98",required=false) Integer w,
			@RequestParam(value="h",defaultValue="33",required=false) Integer h,
			HttpServletResponse response){
		
		System.out.println("获取验证码");
		
		CpachaUtil cpachaUtil = new CpachaUtil(vl, w, h);
		String generatorVCode = cpachaUtil.generatorVCode();
		request.getSession().setAttribute("loginCpacha", generatorVCode);
		System.out.println(generatorVCode);
		BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
		try {
			ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取验证码
	 * @param session
	 * @return
	 */
	@GetMapping(value="getcode")
	@ResponseBody
	public String getCode(HttpSession session){
		String codeString  = (String) session.getAttribute("loginCpacha");
		System.out.println("codeString:"+codeString);
		return codeString;
	}
	
	/**
	 * 按照编号模糊查询所有 关联部门
	 * @param empno
	 * @return
	 */
	@GetMapping("useemp")
	@ResponseBody
	public List<Emp> selectEmpByLike(String empno) {
		//判断如果empno是null转为""
		if(empno == null) {
			empno = "";
		}
		List<Emp> emp = empService.selectEmpByLike(empno);
		emp.forEach(System.out::println);
		for (Emp emp2 : emp) {
			System.out.println(emp2.getDeptids());
		}
		return emp;
		
	}
	
	/**
	 * 查询得到emp总数
	 * @return
	 */
	@GetMapping(value="getCount")
	@ResponseBody
	public int getByCount(){
		int count =empService.getEmpsCount();
		return count;
	}
	
	//退出登录
    @GetMapping(value="remove")
    @ResponseBody
    public void remove(HttpSession session,HttpServletResponse response) throws IOException{
       /* session.removeAttribute("sessionemp");*/
        session.invalidate();
        
        System.out.println("退出登录");
        System.out.println("测试一下");
 
        response.sendRedirect("login.jsp");
    }
	
	
}
